/**
 * 数据列表页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_ROLE_INFO");
    initSelect2();
    //加载数据列表
    loadGrid();
    $("#queryBtn").click(function(){
        query();
    });
    $("#resetBtn").click(function(){
        $('#searchForm')[0].reset();
        $('#searchForm').find('.select2-chosen').html('请选择');
    });
    $("#addBtn").click(function(){
        openAddPage();
    });
});

/**
 * 加载数据列表
 */
function loadGrid(){
    var gridTable = jQuery("#gridTable");
    gridTable.jqGrid({
        mtype: "POST",
        url: springUrl+"/web/roleInfo/page",
        datatype: "json",
        autowidth: true,
        height: "100%",
        colModel: [
            {name:'uniqueNo', label:'uniqueNo', index:'', width:50, sortable: false, resizable:false, hidden: true},
            {name:'roleCode',label:'角色编码', index:'',width:100, sortable: false, align:'left'},
            {name:'roleName',label:'角色名称', index:'', width:100, sortable: false, align:'left'},
            {name:'roleTypeName',label:'角色类型', index:'', width:100, sortable: true, align:'left'},
            {name:'remark',label:'备注', index:'', width:100, sortable: false, align:'left'},
            {name:'', label:'操作', index:'', width:220, fixed:true, sortable:false, resize:false, align:'center',
                formatter:function(cellValue, options, rowObject){
                    var context = "";
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openConfigPage(\''+rowObject.uniqueNo+'\')" title="配置">配置</button> ';
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="openEditPage(\''+rowObject.uniqueNo+'\')" title="修改">修改</button> ';
                    context += '<button class="mfui-btn mfui-btn-primary mfui-btn-sm" onclick="deleteByPrimaryKey(\''+rowObject.uniqueNo+'\')" title="删除">删除</button>';
                    return context;
                }
            }
        ],
        rownumbers: true,//添加左侧行号
        viewrecords : true,
        rowNum: 10,
        rowList: [10,20,30],
        sortname: "create_time",
        sortorder: "desc",
        altRows: true,
        pager : "#gridPager",
        jsonReader: { //server返回Json解析设定
            root: "content", //对于json中数据列表
            page: "pageNum",
            total: "total",
            records: "records"
        }
    });
    //navButtons
    gridTable.jqGrid('navGrid',"#gridPager",
        {
            add: false,
            addicon : 'fa-plus purple',
            edit: false,
            editicon : 'fa-pencil blue',
            del: false,
            delicon : 'fa-trash red',
            view: false,
            viewicon : 'fa-search-plus orange',
            search: false,
            searchicon : 'fa-search grey',
            refresh: true,
            refreshicon : 'fa-refresh green'
        }
    );
    updatePagerIcons(this);
    resizeJqGrid("gridTable");
}

/**
 * 查询
 */
function query(){
    if($('#searchForm').trigger("validate").isValid()){
        var gridParam = {
            url: springUrl+"/web/roleInfo/page",
            postData:{
                roleCode : $("#roleCode").val(),
                roleNameLike : $("#roleNameLike").val(),
                roleTypeCode : $("#roleTypeCode").val()
            },
            page:1
        };
        $("#gridTable").jqGrid("setGridParam", gridParam).trigger("reloadGrid");
    }
}

/**
 * 打开新增页面
 */
function openAddPage(){
    window.forward(springUrl+"/web/roleInfo/addPage");
}

/**
 * 打开修改页面
 */
function openEditPage(uniqueNo){
    window.forward(springUrl+'/web/roleInfo/editPage?uniqueNo='+uniqueNo);
}

/**
 * 打开配置页面（默认资源配置页面）
 */
function openConfigPage(uniqueNo){
    window.forward(springUrl+'/web/roleInfo/resourceConfigPage?uniqueNo='+uniqueNo);
}

/**
 * 根据主键删除
 */
function deleteByPrimaryKey(uniqueNo) {
    Alert.confirm("您确定删除该条信息？", function () {
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: springUrl+"/web/roleInfo/delete",
            data:{
                uniqueNo:  uniqueNo
            },
            success:function (data) {
                Alert.success("执行成功！", function () {
                    $("#gridTable").trigger("reloadGrid");
                });
            },
            error : function(data){
                Alert.error(data.responseText);
            }
        });
    })
}